package cn.com.lyj6851.modules.wechat;

import cn.com.lyj6851.modules.mp.domain.YdyArticle;
import cn.com.lyj6851.modules.mp.service.YdyArticleService;
import cn.com.lyj6851.modules.mp.service.dto.YdyArticleDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyArticleQueryCriteria;
import cn.com.lyj6851.modules.service.WechatArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商城:微信图文管理")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("api")
public class WechatArticleController {
    private final YdyArticleService ydyArticleService;
    private final WechatArticleService wechatArticleService;

    @ApiOperation(value = "查询单条信息")
    @GetMapping(value = "/ydyArticle/info/{id}")
    @PreAuthorize("hasAnyRole('admin','ARTICLE_ALL','ARTICLE_GET')")
    public ResponseEntity getInfo(@PathVariable Integer id){
        return new ResponseEntity<>(ydyArticleService.getById(id), HttpStatus.OK);
    }

    @ApiOperation(value = "查询")
    @GetMapping(value = "/ydyArticle")
    @PreAuthorize("hasAnyRole('admin','ARTICLE_ALL','ARTICLE_SELECT')")
    public ResponseEntity getYxArticles(YdyArticleQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ydyArticleService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @ApiOperation(value = "新增")
    @PostMapping(value = "/ydyArticle")
    @PreAuthorize("hasAnyRole('admin','ARTICLE_ALL','ARTICLE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YdyArticle resources){
        return new ResponseEntity<>(ydyArticleService.save(resources),HttpStatus.CREATED);
    }


    @ApiOperation(value = "修改")
    @PutMapping(value = "/ydyArticle")
    @PreAuthorize("hasAnyRole('admin','ARTICLE_ALL','ARTICLE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YdyArticle resources){
        ydyArticleService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/ydyArticle/{id}")
    @PreAuthorize("hasAnyRole('admin','YDYARTICLE_ALL','YDYARTICLE_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        ydyArticleService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "发布文章")
    @GetMapping(value = "/ydyArticle/publish/{id}")
    @PreAuthorize("hasAnyRole('admin','ARTICLE_ALL','ARTICLE_DELETE')")
    public ResponseEntity publish(@PathVariable Integer id)  throws Exception{
        YdyArticleDto ydyArticleDTO= new YdyArticleDto();
        YdyArticle ydyArticle = ydyArticleService.getById(id);
        BeanUtils.copyProperties(ydyArticle,ydyArticleDTO);
        wechatArticleService.publish(ydyArticleDTO);
        return new ResponseEntity(HttpStatus.OK);
    }
}
